package com.dev.bbs.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dev.bbs.consts.ArticleCate;
import com.dev.bbs.domain.BbsArticle;
import com.dev.bbs.domain.BbsUser;
import com.dev.bbs.mapper.BbsArticleMapper;
import com.dev.bbs.service.IBbsArticleService;
import com.dev.bbs.service.IEsArticleService;
import com.dev.common.core.text.Convert;
import com.dev.common.utils.DateUtils;

/**
 * 社区文章Service业务层处理
 *
 * @author ruoyi
 * @date 2019-09-17
 */
@Service
public class BbsArticleServiceImpl implements IBbsArticleService
{
    @Autowired
    private BbsArticleMapper  bbsArticleMapper;

    @Autowired
    private IEsArticleService esArticleService;

    /**
     * 查询社区文章
     *
     * @param id 社区文章ID
     * @return 社区文章
     */
    @Override
    public BbsArticle selectBbsArticleById(Long id)
    {
        return bbsArticleMapper.selectBbsArticleById(id);
    }

    /**
     * 查询社区文章列表
     *
     * @param bbsArticle 社区文章
     * @return 社区文章
     */
    @Override
    public List<BbsArticle> selectBbsArticleList(BbsArticle bbsArticle)
    {
        return bbsArticleMapper.selectBbsArticleList(bbsArticle);
    }

    /**
     * 新增社区文章
     *
     * @param bbsArticle 社区文章
     * @return 结果
     */
    @Override
    public int insertBbsArticle(BbsArticle bbsArticle)
    {
        bbsArticle.setCreateTime(DateUtils.getNowDate());
        bbsArticle.setStatus(ArticleCate.SHARE.getCode().equals(bbsArticle.getCate()) ? 0 : 1);
        int num = bbsArticleMapper.insertBbsArticle(bbsArticle);
        // 插入es
        if (num > 0)
        {
            esArticleService.insertBbsArticle(bbsArticle);
        }
        return num;
    }

    /**
     * 修改社区文章
     *
     * @param bbsArticle 社区文章
     * @return 结果
     */
    @Override
    public int updateBbsArticle(BbsArticle bbsArticle)
    {
        int num = bbsArticleMapper.updateByPrimaryKeySelective(bbsArticle);
        // 更新es
        if (num > 0)
        {
            BbsArticle updated = selectBbsArticleById(bbsArticle.getId());
            esArticleService.updateBbsArticle(updated);
        }
        return num;
    }

    /**
     * 删除社区文章对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBbsArticleByIds(String ids)
    {
        int num = bbsArticleMapper.deleteBbsArticleByIds(Convert.toStrArray(ids));
        // 成功之后执行es操作
        if (num > 0)
        {
            String[] idArr = Convert.toStrArray(ids);
            for (String id : idArr)
            {
                esArticleService.deleteBbsArticleById(Long.valueOf(id));
            }
        }
        return num;
    }

    /**
     * 删除社区文章信息
     *
     * @param id 社区文章ID
     * @return 结果
     */
    public int deleteBbsArticleById(Long id)
    {
        int num = bbsArticleMapper.deleteBbsArticleById(id);
        // 删除es
        if (num > 0)
        {
            esArticleService.deleteBbsArticleById(id);
        }
        return num;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.dev.bbs.service.IBbsArticleService#deleteBbsArticle(java.lang.Long,
     * com.dev.bbs.domain.BbsUser)
     */
    @Override
    public int deleteBbsArticle(Long id, BbsUser bbsUser)
    {
        // 管理员直接删除，非管理员只能删除自己的帖子
        BbsArticle bbsArticle = new BbsArticle().setId(id);
        if (!bbsUser.getAdmin())
        {
            bbsArticle.setAuthor(bbsUser.getUserId());
        }
        int num = bbsArticleMapper.logicDeleteArticle(bbsArticle);
        // 删除es
        if (num > 0)
        {
            esArticleService.deleteBbsArticleById(id);
        }
        return num;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.dev.bbs.service.IBbsArticleService#view(long)
     */
    @Override
    public boolean view(long id)
    {
        return bbsArticleMapper.view(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.dev.bbs.service.IBbsArticleService#addComment(java.lang.Long,
     * int)
     */
    @Override
    public int addComment(Long id, int num)
    {
        return bbsArticleMapper.addComment(id, num);
    }
}
